var amqp = require('amqplib');
const MongoClient = require('mongodb').MongoClient;
const {MONGO_URL,AMQP_ADDRESS,QUEUE_SINGLE_WK_NAME,MONGO_DB_NAME,MONGO_TABLE_SINGLE_NAME} = require('../config.js')
const fs = require('fs');

// DATA schema
// {
//   _id: name keywork
//   name:
//   keyword:
//   name_match:
//   keyword_match:
//   baidu_match:
// }

let KEY_WORDS = [];
(()=>{
  let data = fs.readFileSync('./data/n.txt','utf8').split('\n')
  let tmp_set = {}
  for (i in data){
    tmp_set[data[i].trim()] = null
  }
  for (i in tmp_set){
    KEY_WORDS.push(i)
  }
})();

// 获取所有公司名
(()=>{
    let data = fs.readFileSync('./data/c.txt','utf8').split('\n')
    let tmp_set = {}
    for (i in data){
      tmp_set[data[i].trim()] = null
    }
    for (i in tmp_set){
      KEY_WORDS.push(i)
    }
})();



const product = async () => {
  let conn = await amqp.connect(AMQP_ADDRESS)
  let ch = await conn.createChannel()
  try{ch.deleteQueue(QUEUE_SINGLE_WK_NAME)} catch(err){console.log(err)}

  let q = await ch.assertQueue(QUEUE_SINGLE_WK_NAME, {durable: false});
  while(true){
    let client = await MongoClient.connect(MONGO_URL, {useNewUrlParser: true});
    let db = client.db(MONGO_DB_NAME);
    try {
      for (keyword of KEY_WORDS){
        keyword = keyword.trim()
        search_key = `"${keyword}"`
        if (search_key.length>40){
          console.log('lenght > 40',search_key)
          continue
        }

        let doc = await db.collection(MONGO_TABLE_SINGLE_NAME).countDocuments({'keyword': keyword});
        if (doc){
          continue
        }

        let data = {keyword:keyword}
        ch.sendToQueue(q.queue,Buffer.from(JSON.stringify(data)));
      }


    } catch (err) {
      if (err.name == "MongoError" && err.codeName == "CursorNotFound") {
        alert( "CursorNotFound" + e.message );
        client.close();
        continue
      } else {
        console.error(err); // rethrow (*)
      }
    } finally {
      // client.close();
      break
      client.close();
    }
  }
  client.close()
  conn.close()
}




product()
